<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\goods\service;

use app\goods\model\GoodsModel;

class GoodsService
{

    public function adminGoodsList($filter)
    {
        return $this->adminPostList($filter);
    }

    public function adminPostList($filter, $isPage = false)
    {
        //每页数据条数
        $pageSize = empty($filter['page_size']) ? 10 : intval($filter['page_size']);

        //默认使用前台排序规则
        $order    = $filter['sort_by'].' '.$filter['list_order'];
        if( empty($filter['sort_by']) ){
            $order = 'is_top DESC,b.list_order DESC,published_time DESC,goods_id DESC';
        }

        //查询条件
        $where = [
            'a.create_time' => ['>=', 0],   //创建时间
            'a.delete_time' => 0            //删除时间
        ];

        $where['a.site_id'] = get_site_id();  //站点ID

        //审核状态
        if( !empty($filter['post_status']) ){
            if( $filter['post_status'] == 1 ){
                $where['a.post_status'] = [['>=',0], ['<',99]];     // 待审核(0--98)
            }else{
                $where['a.post_status'] = $filter['post_status'];   // 审核通过(99)或不通过(-1)
            }
        }
        
        //连表
        $join = [
            ['__USER__ u', 'a.user_id = u.id'],
            ['__GOODS_CATEGORY_RELATE__ b', 'a.goods_id = b.goods_id'],
        ];

        //要查询的字段
        $field = 'a.*,b.id AS goods_category_id,b.list_order,b.category_id, u.user_login,b.is_show';

        //是否指定查询某个分类
        $category = empty($filter['category']) ? 0 : intval($filter['category']);
        if (!empty($category)) {
            $where['b.category_id'] = ['eq', $category];
            
        }else{
            /*若不是超级管理员，只获取该角色拥有的操作分类下的文章*/
            if( session('ROLE_ID') != 1 ){
                /*获得有权限的分类id*/
                $idList                 = get_cat_priv('init', 'goods_category'); //获取该角色可查看的栏目分类id
                $where['b.category_id'] = ['in',$idList];
            }
        }


        $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime   = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.published_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.published_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.published_time'] = ['<= time', $endTime];
            }
        }

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if (!empty($keyword)) {
            $where['a.goods_name'] = ['like', "%$keyword%"];
        }

        $goodsModel = new GoodsModel();
        $goodsList  = $goodsModel->alias('a')->field($field)
                   -> join($join)
                   -> where($where)
                   -> order($order)
                   -> paginate($pageSize);
        
        // echo $goodsModel->getLastSql();die;
        return $goodsList;

    }

    public function publishedGoods($goodsId, $categoryId = 0)
    {
        $goodsModel = new GoodsModel();

        if (empty($categoryId)) {

            $where = [
                // 'goods.post_type'      => 1,
                'goods.published_time' => [['< time', time()], ['> time', 0]],
                // 'goods.post_status'    => 99,
                'goods.delete_time'    => 0,
                'goods.goods_id'       => $goodsId
            ];

            $goods = $goodsModel->alias('goods')->field('goods.*')
                ->where($where)
                ->find();
        } else {
            $where = [
                // 'goods.post_type'       => 1,
                'goods.published_time' => [['< time', time()], ['> time', 0]],
                // 'goods.post_status'    => 99,
                'goods.delete_time'    => 0,
                'relation.category_id' => $categoryId,
                'relation.goods_id'    => $goodsId
            ];

            $join    = [
                ['__GOODS_CATEGORY_RELATE__ relation', 'goods.goods_id = relation.goods_id']
            ];
            $goods = $goodsModel->alias('goods')->field('goods.*')
                ->join($join)
                ->where($where)
                ->find();
        }


        return $goods;
    }


}
